﻿using Abp.Domain.Repositories;
using Abp.EntityFramework;
using AVIS.OutBound.AWD;
using AVIS.OutBound.EntityFramework.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AVIS.OutBound.EntityFramework.Repositories
{
    public class AWDRepository : OutBoundRepositoryBase<AWDNumber>, IAWDRepository
    {
        private readonly IRepository<AWDNumber> _AWDRepository;
        private readonly IRepository<tblBinCode> _BinCodeRepository;
        public AWDRepository(IDbContextProvider<OutBoundDbContext> dbContextProvider,
            IRepository<AWDNumber> awdRepository,
            IRepository<tblBinCode> binCodeRepository)
            : base(dbContextProvider)
        {
            _AWDRepository = awdRepository;
            _BinCodeRepository = binCodeRepository;
        }

        public AWDNumber GetAWDNumber(string awdCode)
        {
            var AWDNumber = _AWDRepository.FirstOrDefault(a => a.Code == awdCode || a.CNAWD == awdCode);
            if (AWDNumber != null)
            {
                var binCodes = from b in _BinCodeRepository.GetAll()
                               where b.awd == awdCode
                               select b.binCode;

                AWDNumber.BinCodes = binCodes.ToArray();
            }

            return AWDNumber;
        }
    }
}
